package com.lightsaber.trade.as.web.retail;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springside.modules.orm.Page;

import com.lightsaber.trade.as.entity.reta.OrderItem;
import com.lightsaber.trade.as.entity.reta.Product;
import com.lightsaber.trade.as.service.reta.OrderItemManager;
import com.lightsaber.trade.as.service.reta.OrderManager;
import com.lightsaber.trade.as.service.reta.ProductManager;
import com.lightsaber.trade.core.web.BaseAction;

@Namespace("/retail")
public class OrderItemAction extends BaseAction implements SessionAware  {

    private Map<String, Object> session;

    @Autowired
    private OrderItemManager orderItemManager;

    @Autowired
    private OrderManager orderManager;

    @Autowired
    private ProductManager productManager;

    // -------------------------------------product search START
    @Value(value = "#{appProperties['retail.page.size']}")
    private String PAGE_SIZE;
    private Page<Product> page;
    private Product product;
    private String[] ids;
    public static final String SELECTED_PRODUCT_FOR_ORDER_MAP = "SELECTED_PRODUCT_FOR_ORDER_MAP";
    // -------------------------------------product search END

    private String orderId;
    private boolean clear;
    private OrderItem orderItem;
    private List<OrderItem> results;

    @Override
    public String execute() throws Exception {
        if (clear) {
            orderItem = null;
        }

        results = orderItemManager.findPageByOrder(orderId);
        return SUCCESS;
    }

    public String delete() throws Exception {
        orderItemManager.delete(orderItem.getId());
        orderManager.onItemChanged(orderId);
        
        return execute();
    }

    public String batchDelete() throws Exception {
        orderItemManager.delete(ids);
        orderManager.onItemChanged(orderId);
        
        return execute();
    }

    public String saveNumber() throws Exception {
        orderItemManager.saveNumber(results);
        orderManager.onItemChanged(orderId);
        
        return execute();
    }

    public String add() throws Exception {
        // 第一次进入这个页面要清楚session
        session.remove(SELECTED_PRODUCT_FOR_ORDER_MAP);

        return searchProducts();
    }

    public String searchProducts() throws Exception {
        getSelectedCodesMap();

        if (clear) {
            product = null;
        }

        if (page == null) {// Click 'Search' button first
            page = new Page<Product>(Integer.valueOf(PAGE_SIZE));
        } else {// Click 'Search' button
            page.setPageSize(Integer.valueOf(PAGE_SIZE));
        }

        if (!page.isOrderBySetted()) {
            page.setOrderBy("name");
            page.setOrder(Page.ASC);
        }

        productManager.findPage(product, page);

        return INPUT;
    }

    public String confirmSelectedProducts() throws Exception {
        Map<String, String> selectedIdsMap = getSelectedCodesMap();

        orderItemManager.confirmSelectedProducts(orderId, selectedIdsMap);
        session.remove(SELECTED_PRODUCT_FOR_ORDER_MAP);
        orderManager.onItemChanged(orderId);
        
        return execute();
    }

    public OrderItem getOrderItem() {
        return orderItem;
    }

    public void setOrderItem(OrderItem orderItem) {
        this.orderItem = orderItem;
    }

    public boolean isClear() {
        return clear;
    }

    public void setClear(boolean clear) {
        this.clear = clear;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public List<OrderItem> getResults() {
        return results;
    }

    public void setResults(List<OrderItem> results) {
        this.results = results;
    }

    public Page<Product> getPage() {
        return page;
    }

    public void setPage(Page<Product> page) {
        this.page = page;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    @Override
    public void setSession(final Map<String, Object> session) {
        this.session = session;
    }

    public String[] getIds() {
        return ids;
    }

    public void setIds(String[] ids) {
        this.ids = ids;
    }

    private Map<String, String> getSelectedCodesMap() {
        Map<String, String> selectedIdsMap = (Map<String, String>) session
                .get(SELECTED_PRODUCT_FOR_ORDER_MAP);

        if (selectedIdsMap == null) {
            selectedIdsMap = new HashMap<String, String>();
            session.put(SELECTED_PRODUCT_FOR_ORDER_MAP, selectedIdsMap);
        }

        if (ids != null && ids.length > 0) {
            for (String id : ids) {
                selectedIdsMap.put(id, id);
            }
        }

        return selectedIdsMap;
    }

}
